<?php

namespace common\models\views;

use Yii;
use common\helpers\Url;
use common\models\shop\ShopProduct;
use common\models\shop\ShopGoods;

/**
 * This is the model class for table "view_sku_spec".
 *
 * @property int $id
 * @property int $product_id 货品ID
 * @property int $goods_id 商品ID
 * @property int $spec_value_id 规格值ID
 * @property string $spec_value 规格值
 * @property int $spec_id 规格ID
 * @property string $spec_name 规格名称
 * @property int $spec_screen 是否筛选项
 */
class ViewSkuSpecMin extends \common\db\ActiveRecord {

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return 'view_sku_spec_min';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['id', 'product_id', 'goods_id', 'spec_value_id', 'spec_id', 'spec_screen', 'total', 'shelf', 'stock', 'status'], 'integer'],
            [['spec_value', 'spec_name', 'goods_no', 'name'], 'required'],
            [['price', 'discount', 'cost_price', 'market_price'], 'number'],
            [['spec_value', 'name'], 'string', 'max' => 50],
            [['spec_name'], 'string', 'max' => 20],
            [['goods_no'], 'string', 'max' => 16],
            [['cover'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'product_id' => '货品ID',
            'goods_id' => '商品ID',
            'spec_value_id' => '规格值ID',
            'spec_value' => '规格值',
            'spec_id' => '规格ID',
            'spec_name' => '规格名称',
            'spec_screen' => '是否筛选项',
            'goods_no' => '商品货号',
            'cover' => 'Cover',
            'total' => 'Total',
            'shelf' => 'Shelf',
            'stock' => 'Stock',
            'price' => 'Price',
            'discount' => 'Discount',
            'cost_price' => 'Cost Price',
            'market_price' => 'Market Price',
            'name' => 'Name',
            'status' => 'Status',
        ];
    }

    /**
     * 关联SKU单品信息
     */
    public function getGoods() {
        return $this->hasOne(ShopGoods::className(), ['id' => 'goods_id']);
    }

    /**
     * 获取商品SKU面板单品展示封面
     */
    public function getImgUrl() {
        return Url::attach($this->cover);
    }

    public static function sku($id) {
        $query = static::find()->alias('t')->where(['t.product_id' => $id]);
        $query->innerJoinWith([
            'goods g' => function ($query) {
                $query->active(ShopGoods::STATUS_ACTIVE);
            },
        ], false);
        return $query->all();
    }

    public function getTag() {
        return $this->spec_value_id;
    }

    /**
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'id' => 'spec_value_id',
            'k_id' => 'spec_id',
            'k' => 'spec_name',
            'k_s' => function() {
                return 's' . $this->spec_id;
            },
            'name' => 'spec_value',
            'imgUrl'
        ];
    }

}
